"""
__author__ = 'heyanmin'
__time__ = '2021/4/18 4:12 下午'
"""
import time

import yaml
from selenium import webdriver


class BasePage:
    # 初始化操作，包含driver复用 赋值 全局等待设置等等
    # 没有参数传入，默认取None ；有传入参数，取传入的参数
    def __init__(self,base_driver=None):
        # 如果 base_driver 是初始值None，那么就会实例化driver
        if base_driver is None:
            # 实例化driver
            self.driver = webdriver.Chrome()
            # 设置隐示等待时间
            self.driver.implicitly_wait(5)
            # 访问企业微信扫码登录网页
            self.driver.get("https://work.weixin.qq.com/wework_admin/loginpage_wx")
            time.sleep(5)
            # 存储cookie
            # cookie = self.driver.get_cookies()
            # print(cookie)
            # with open("cookie.yml",'w',encoding="UTF-8") as f:
            #     yaml.dump(cookie,f)

            # # # 读取cookie
            with open("cookie.yml") as f:
                datas = yaml.safe_load(f)
                # print(datas)
                for cookie in datas:
                    self.driver.add_cookie(cookie)
                    # print(cookie)
            self.driver.get("https://work.weixin.qq.com/wework_admin/frame#index")
            # 隐示等待，3s内找到元素执行，找不到报错
            self.driver.implicitly_wait(3)
        else:
            self.driver = base_driver

    def find(self,by,ele = None):
        """
        :param by: 定位方式如 css id
        :param ele: 元素定位信息
        :return:
        """
        # *作用解元祖，等同于 *self.ele_name = (By.ID, "username")
        if ele == None:
            return self.driver.find_element(*by)
        else:
            return self.driver.find_element(by,ele)



